package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.bean.TableTypeListBean;

import java.util.ArrayList;
import java.util.logging.Handler;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

public class SelectTypeAdapter extends RecyclerView.Adapter<SelectTypeAdapter.ViewHolder> {
    private Activity activity;
    private ArrayList<TableTypeListBean> jsonBean;

    public SelectTypeAdapter(Activity activity, ArrayList<TableTypeListBean> jsonBean) {
        this.activity = activity;
        this.jsonBean = jsonBean;
    }

    @Override
    public SelectTypeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_select, parent, false);
        SelectTypeAdapter.ViewHolder holder = new SelectTypeAdapter.ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(SelectTypeAdapter.ViewHolder holder, final int i) {
        TableTypeListBean tableTypeListBean = jsonBean.get(i);
        holder.tv_name.setText(tableTypeListBean.getName());
        if (tableTypeListBean.isCheck()) {
            holder.tv_name.setTextColor(activity.getResources().getColor(R.color.white));
            holder.tv_name.setBackgroundResource(R.color.blue_5e);
        } else {
            holder.tv_name.setTextColor(activity.getResources().getColor(R.color.black));
            holder.tv_name.setBackgroundResource(R.color.white);
        }
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tableTypeListBean.setCheck(true);
                for (TableTypeListBean tableTypeListBean1 :jsonBean){
                    if (!tableTypeListBean.getName().equals(tableTypeListBean1.getName())){
                        tableTypeListBean1.setCheck(false);
                    }
                }
                notifyDataSetChanged();
                if (listener != null){
                    listener.onItemClick(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return jsonBean.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        RelativeLayout ll_item;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            ll_item = itemView.findViewById(R.id.ll_item);
        }
    }
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
